/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.registration.gui.security;

import it.polimi.registration.business.security.boundary.CalendarManager;
import it.polimi.registration.business.security.boundary.UserManager;
import it.polimi.registration.business.security.entity.Event;
import it.polimi.registration.business.security.entity.Invitation;
import it.polimi.registration.business.security.entity.Notification;
import it.polimi.registration.business.security.entity.User;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.GregorianCalendar;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author miglie
 */
@Named
@RequestScoped
public class UserViewBean {

	@EJB
	UserManager um;

	@EJB
	CalendarManager cm;

	@PersistenceContext
	EntityManager em;

	private User u;

	public UserViewBean() {
	}

	@PostConstruct
	public void init() {
		try {
			String value = FacesContext.getCurrentInstance()
					.getExternalContext().getRequestParameterMap().get("user");
			int id = Integer.parseInt(value);
			u = um.get(id);

		} catch (Exception ex) {
			if (u == null) {
				try {
					FacesContext.getCurrentInstance().getExternalContext()
							.redirect("index.xhtml?faces-redirect=true");
				} catch (IOException e) {
					e.printStackTrace();
				} finally {

				}
			}
		}
	}

	public String getName() {
		return u.getFirstName() + " " + u.getLastName();
	}

	public String getEmail() {
		return u.getEmailAddress();
	}

	public String getAvatar() {
		if (u.getAvatar() == null || u.getAvatar().toString().isEmpty())
			return "/imgs/profile.png";
		else
			return u.getAvatar().toString();
	}

	public String getVisibilityCalendar() {
		return u.getCalendar().isPublic() ? "Public" : "Private";
	}

	public String getInverseVisibilityCalendar() {
		return !u.getCalendar().isPublic() ? "public" : "private";
	}

	public boolean getIsPublic() {
		return u.getCalendar().isPublic();
	}

	public List<Event> getNextEvents() {
		List<Event> le = u.getEvents();
		List<Event> res = new ArrayList<Event>();
		Calendar now = new GregorianCalendar();

		res.addAll(this.getMyEvents());
		res.addAll(this.getAcceptedEvents());

		sortList(res);

		return res;
	}

	public List<Event> getMyEvents() {
		// List<Event> le = getNextEvents(u);
		List<Event> mine = new ArrayList<>();
		Calendar now = new GregorianCalendar();

		/*
		 * for(Event e : le) { if(e.getOwner().equals(u)) { mine.add(e); } }
		 */

		List<Event> le = (List<Event>) em
				.createQuery("SELECT t FROM event t where t.owner = :value1")
				.setParameter("value1", u).getResultList();

		for (Event e : le) {
			if (now.before(e.getDate())) {
				mine.add(e);
			}
		}

		sortList(mine);

		return mine;
	}

	public List<Event> getAcceptedEvents() {
		List<Event> res = getMyEvents();
		List<Invitation> ln;
		Calendar now = new GregorianCalendar();

		/*
		 * for(Notification n : ln) { if(ln instanceof Invitation) {
		 * if(((Invitation)n).getAnswer()) {
		 * if(now.before(n.getEvent().getDate())) res.add(n.getEvent()); } } }
		 */

		ln = em.createNativeQuery(
				"SELECT * FROM notification where OWNER_ID = " + u.getId()
						+ " and TYPE_ENTITE = 'Invitation' and answer = 1",
				Invitation.class).getResultList();

		for (Invitation n : ln) {
			if (now.before(n.getEvent().getDate()))
				res.add(n.getEvent());
		}

		sortList(res);

		return res;
	}

	private void sortList(List<Event> l) {
		l.sort(new Comparator<Event>() {

			@Override
			public int compare(Event arg0, Event arg1) {
				return arg0.compareTo(arg1);
			}
		});
	}

	public int getNotifsUnreadNumber() {
		List<Notification> ln = u.getNotifications();
		int i = 0;

		for (Notification n : ln)
			if (!n.isRead())
				i++;

		return i;
	}
}
